package fileexplorer.commands;

import java.util.ArrayList;


import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.swt.dnd.Clipboard;
import org.eclipse.swt.dnd.FileTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.handlers.HandlerUtil;

import fileexplorer.FileObject;
import fileexplorer.Tools;
import fileexplorer.View;

public class Copier extends AbstractHandler {

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {

		IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event);
		
		IWorkbenchPage page = window.getActivePage();
		
	    View view = (View) page.findView(View.ID);
	    
	    copy(view);
	    
		return null;
	}
	
	public static void copy(View view) {
		ArrayList<FileObject> files = view.getFileObjectSelected();
		System.out.println(files.get(0).getPath());
		
		Clipboard clipboard = new Clipboard(view.shell.getDisplay());
		System.out.println(Tools.getDisplay());
		
		String[] data = new String[files.size()];

		for (int i = 0; i < data.length; i++) {
			data[i] = files.get(i).getPath();
		}
		System.out.println(data);
        clipboard.setContents(new Object[] { data },
                new Transfer[] { FileTransfer.getInstance() });
        clipboard.dispose();
	}
	
	

}
